const { authJWT } = require('@root/config/auth');
const ServerResponse = require('@root/middlewares/resHandler');
const articleController = require('@root/controllers/article.controller')
const articleValidator = require('@root/validators/article.validator')

const Router = require('koa-router');
const router = Router();
const Article = require('@root/models/article.model');

router.param('article',async(slug,ctx,next)=>{
    const article = await Article.findOne({ slug}).populate('author');
  
    if (!article) return ctx.body = ServerResponse.createByErrorMsg('该文章不存在') ;
  
    ctx.state.article = article;
  
    return next();
})
router.get('/articles', authJWT.required,articleController.getAll)
router.post('/articles', authJWT.required, articleValidator.createArticle, articleController.createOne)
router.get('/articles/:article', authJWT.required,articleController.getOne)
router.put('/articles/:article', authJWT.required,articleValidator.updateArticle,articleController.updateOne)
router.delete('/articles/:article', authJWT.required,articleController.deleteOne)
router.post('/articles/deleteMany', authJWT.required,articleController.deleteMany)


router.post('/articles/:article/favorite', authJWT.required,articleController.favorite)
router.delete('/articles/:article/favorite', authJWT.required,articleController.unFavorite)


router.post('/articles/:article/comment', authJWT.required,articleController.addComment)
router.delete('/articles/:article/:comment', authJWT.required,articleController.deleteComment)
router.get('/articles/:article/comments', authJWT.required,articleController.getComments)
module.exports = router.routes();
